//
//  LAWriteReviewPictureTableViewCell.m
//  LYLazizDevelopDemo
//
//  Created by xbn_iMac on 2016/11/4.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LAWriteReviewPictureTableViewCell.h"
#import "UIButton+EnlargeEdge.h"
#import "Masonry.h"

@interface LAWriteReviewPictureTableViewCell ()

@property (weak, nonatomic) IBOutlet UIView *pictureBGView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureViewHeightConstraint;

@property (strong, nonatomic) NSMutableArray *arrImageViews;

@property (strong, nonatomic) NSMutableArray *arrDeleteButtons;

@property (assign, nonatomic) CGFloat scale;

@property (strong, nonatomic) UIImageView *addImageView;



@end

@implementation LAWriteReviewPictureTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.arrImages = [NSMutableArray array];
    self.arrImageViews = [NSMutableArray array];
    self.arrDeleteButtons = [NSMutableArray array];
    self.scale = SCREEN_WIDTH/375.0;
    [self createAddImageView];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - 创建添加图片按钮的view
-(void)createAddImageView {
    self.addImageView = [[UIImageView alloc] init];
    self.addImageView.image = [UIImage imageNamed:@"添加图片_点评"];
    self.addImageView.userInteractionEnabled = YES;
    [self.contentView addSubview:self.addImageView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTapsRequired = 1;
    [self.addImageView addGestureRecognizer:tap];
    
    [self addNewImageViewConstraints:self.addImageView andIndex:1];
}

#pragma mark - 单点手势处理事件
-(void)tapAction:(UITapGestureRecognizer*)tap {
    if (self.addNewPictureBlock) {
        self.addNewPictureBlock();
    }
}

#pragma mark - 添加新图片
- (void)addNewPicture:(UIImage*)aImage {
    [self createSubImageView:self.arrImageViews.count+1 andImage:aImage];
}

#pragma mark - 创建新的图片view
-(void)createSubImageView:(NSInteger)index andImage:(UIImage*)aImage {
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.backgroundColor = [UIColor redColor];
    imageView.userInteractionEnabled = YES;
    imageView.tag = self.arrImageViews.count;
    imageView.layer.cornerRadius = 4;
    imageView.clipsToBounds = YES;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.alpha = 0;
    imageView.image = aImage;
    [self.contentView addSubview:imageView];
    [self.arrImageViews addObject:imageView];
    [self.arrImages addObject:aImage];
    
    [self createDeleteButton:imageView];
    
    [self addNewImageViewConstraints:imageView andIndex:index];
    
    if (self.haveDataBlock) {
        self.haveDataBlock(self.arrImages.count>0);
    }
}

#pragma mark - 创建删除按钮
-(void)createDeleteButton:(UIImageView*)imageView {
    UIButton *btnDel = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnDel setImage:[UIImage imageNamed:@"comment_delete"] forState:UIControlStateNormal];
    [btnDel addTarget:self action:@selector(deleteButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    btnDel.tag = imageView.tag;
    [btnDel setEnlargeEdge:20];
    [self.contentView addSubview:btnDel];
    [self.arrDeleteButtons addObject:btnDel];
    
    [btnDel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imageView.mas_trailing).offset(-4);
        make.centerY.equalTo(imageView.mas_top).offset(6);
    }];
}

#pragma mark - 删除按钮点击事件
-(void)deleteButtonClickAction:(UIButton*)sender {
    NSLog(@"^^^^^");
    UIImageView *imageView = self.arrImageViews[sender.tag];
    [imageView removeFromSuperview];
    [self.arrImageViews removeObject:imageView];
    [self.arrImages removeObjectAtIndex:sender.tag];
    
    [self updataAfterViewConstraints:sender.tag];
    [self.arrDeleteButtons removeObject:sender];
    [sender removeFromSuperview];
    [self updataSubviewTag];
    
    if (self.haveDataBlock) {
        self.haveDataBlock(self.arrImages.count>0);
    }
}

#pragma mark - 根据位置添加新图片view的约束
- (void)addNewImageViewConstraints:(UIImageView*)imageView andIndex:(NSInteger)index {
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@20);
        make.width.and.height.mas_equalTo(60*self.scale);
        make.leading.mas_equalTo((60*(index-1)+12.5*index)*self.scale);
    }];
    
    [self layoutIfNeeded];//如果其约束还没有生成的时候需要动画的话，就请先强制刷新后才写动画，否则所有没生成的约束会直接跑动画
    
    if (index < 5) {    // 如果是小于5个图片则执行约束动画，否则就执行alpha动画
        [self.addImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo((60*self.arrImageViews.count+12.5*(self.arrImageViews.count+1))*self.scale);
        }];
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        imageView.alpha = 1;
        if (index == 5) {
            self.addImageView.alpha = 0;
        }
        [self layoutIfNeeded];
    }];
}

#pragma mark 当删除时更新删除位置后边图片view的约束
- (void)updataAfterViewConstraints:(NSInteger)index {
    
    [self layoutIfNeeded];

    if (index < 5) {    // 如果是小于5个图片则执行约束动画，否则就执行alpha动画
        [self.addImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo((60*self.arrImageViews.count+12.5*(self.arrImageViews.count+1))*self.scale);
        }];
    }
    
    for (NSInteger i = index; i<self.arrImageViews.count; i++) {
        UIImageView *imageView = self.arrImageViews[i];
        [imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo((60*i+12.5*(i+1))*self.scale);
        }];
    }
    
    
    [UIView animateWithDuration:0.3 animations:^{
        if (index < 5) {
            self.addImageView.alpha = 1;
        }
        [self layoutIfNeeded];
    }];
}

#pragma mark - 更新subview的tag值
-(void)updataSubviewTag {
    for (int i = 0; i<self.arrImageViews.count; i++) {
        UIImageView *imageView = self.arrImageViews[i];
        UIButton *btnDelete = self.arrDeleteButtons[i];
        imageView.tag = i;
        btnDelete.tag = i;
    }
}

+(CGFloat)cellHeight {
    
    return 40+60*SCREEN_WIDTH/375.0;
}


@end
